﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.CodeDom;
using System.Configuration;
using System.Collections;


public partial class reports_userControls_Channels : System.Web.UI.UserControl
{
    private String _TopLevel = String.Empty;
    static Int32 MAX_I = 0, MAX_O = 0;

    public String TopLevel
    {
        get { return _TopLevel; }
        set { _TopLevel = value; }
    }

    private String _RollUpUnit = String.Empty;

    public String RollUpUnit
    {
        get { return _RollUpUnit; }
        set { _RollUpUnit = value; }
    }

    private string _RollupTitle = String.Empty;

    public string RollupTitle
    {
        get { return _RollupTitle; }
        set
        {

            _RollupTitle = value.Split('(')[0];

        }
    }


    protected String CurrProdLine = String.Empty, CurrProd = String.Empty;
    Int32 ALTCHNL_IN = 0, COR_IN = 0, CS_IN = 0, D2D_IN = 0, MOBILITYDMD_IN = 0, NATIONALRET_IN = 0, OBTM_IN = 0, ONLINE_IN = 0, P2RCALLCEN_IN = 0, STORES_IN = 0, TE_IN = 0, WEB_IN = 0, TOT_I = 0;
    Int32 ALTCHNL_OUT = 0, COR_OUT = 0, CS_OUT = 0, D2D_OUT = 0, MOBILITYDMD_OUT = 0, NATIONALRET_OUT = 0, OBTM_OUT = 0, ONLINE_OUT = 0, P2RCALLCEN_OUT = 0, STORES_OUT = 0, TE_OUT = 0, WEB_OUT = 0, TOT_O = 0;
    Decimal ALTCHNL_PCT = 0, COR_PCT = 0, CS_PCT = 0, D2D_PCT = 0, MOBILITYDMD_PCT = 0, NATIONALRET_PCT = 0, OBTM_PCT = 0, ONLINE_PCT = 0, P2RCALLCEN_PCT = 0, STORES_PCT = 0, TE_PCT = 0, WEB_PCT = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            RollupMetadata meta = (RollupMetadata)HttpContext.Current.Session["RollupMetadata"];
            tblCharts.Style["display"] = meta.ChkStacked.Checked || meta.chkShowSummary.Checked ? "" : "none";
            tblTables.Style["display"] = meta.chkShowTables.Checked ? "" : "none";

            if (meta.MonthsPrior > 0)
                litMonth.Text = "&nbsp;&mdash;&nbsp;" + meta.SalesMonth;
            else
                litMonth.Text = ""; 
            
            string TopLevelFilter = String.Format("{1}='{0}'", RollUpUnit.Replace("'", "''"), TopLevel);

            String ManagerLevel = String.Empty;
            String RollUpFilter = String.Empty;

            switch (TopLevel)
            {
                case "Manager":
                    String[] tmpArr = RollUpUnit.Split(':');


                    string RollUpMgr = tmpArr[0];
                    ManagerLevel = tmpArr.Length == 1 ? "0" : tmpArr[1];

                    switch (ManagerLevel)
                    {
                        case "1":
                            //SQLFilter = String.Format(" B.TRAC_ID IN (SELECT TRAC_ID FROM MIS.ACC_TRAC_INFO WHERE AREA_MGR='{0}' GROUP BY 1)", RollUpFilter);
                            DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByAreaMgr(RollUpMgr);
                            DisplayGrid.DataBind();
                            break;

                        case "2":
                            DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByAcctMgr(RollUpMgr);
                            DisplayGrid.DataBind();
                            break;


                        case "0":
                            RollUpFilter = ConfigurationManager.AppSettings[RollUpMgr];
                            DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByRegion(RollUpFilter);
                            DisplayGrid.DataBind();
                            break;

                        default:
                            break;
                    }
                    //Heading1.Text = RollUpMgr + " ROLL UP";
                    break;

                case "State":
                    if (RollUpUnit == "ZZ")
                    {
                        RollUpFilter = ConfigurationManager.AppSettings[RollUpUnit];
                        //SQLFilter = String.Format(" B.REGION='{0}'", RollUpFilter);
                        RollupTitle = RollUpFilter;
                        DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByRegion(RollUpFilter);
                        DisplayGrid.DataBind();
                        break;
                    }
                    //SQLFilter = String.Format(" SUBSTR(B.TRAC_ID,1,2) = '{0}'", RollUpUnit);
                    //Heading1.Text = (String)((Hashtable)Cache["ACC_STATES"])[RollUpUnit] + " ROLL UP";
                    //Heading1.Text = RollUpUnit + " ROLL UP";
                    DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByState(RollUpUnit);
                    DisplayGrid.DataBind();
                    break;

                case "Market":
                    DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByMarket(RollUpUnit);
                    DisplayGrid.DataBind();
                    break;

                case "Portfolio":
                case "Portfolios":
                    DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByPortfolio(RollUpUnit);
                    DisplayGrid.DataBind();
                    break;

                case "TRAC_ID":
                    DisplayGrid.DataSource = (new ChannelSalesByPlan()).ByTRAC_ID(RollUpUnit); ;
                    DisplayGrid.DataBind();


                    //Heading1.Text = RollUpUnit.ToUpper() + " ROLL UP";
                    break;

                default:
                    break;
            }

        }

    }

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (CurrProdLine != e.Row.Cells[1].Text)
            {

                CurrProdLine = e.Row.Cells[1].Text;
            }

            PickImage(e);

        }

        //if (e.Row.RowType == DataControlRowType.Footer)
        //{
        //    PickImage(e);
        //}

        DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    private void ShowSubtotalsImage()
    {

        return;

        //if (CurrProdLine != null && CurrProdLine.Length > 0)
        //{
        //    GridViewRow dr = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Edit);
        //    TableCell dc = new TableCell(); dc.ID = "PRODUCT"; dc.Text = CurrProdLine; dr.Cells.Add(dc);
        //    dc = new TableCell(); dc.ID = "FEATURE"; dc.Text = "SUBTOTAL"; dr.Cells.Add(dc);
        //    dc = new TableCell(); dc.ID = "MTD_CNT"; dc.Text = SUB_MTD_CNT.ToString(); dr.Cells.Add(dc);
        //    dc = new TableCell(); dc.ID = "PREV_MTD_CNT"; dc.Text = SUB_PREV_MTD_CNT.ToString(); dr.Cells.Add(dc);
        //    dc = new TableCell(); dc.ID = "PREV_MON_CNT"; dc.Text = SUB_PREV_MON.ToString(); dr.Cells.Add(dc);
        //    dc = new TableCell(); dc.ID = "PACING_CNT"; dc.Text = SUB_PACING_CNT.ToString(); dr.Cells.Add(dc);
        //    dc = new TableCell(); dc.ID = "PACE_RATE"; dc.Text = Convert.ToDecimal(Convert.ToDecimal(SUB_PACING_CNT) / (Convert.ToDecimal(SUB_PREV_MON) + Convert.ToDecimal(.01))).ToString("p0"); ; dr.Cells.Add(dc);
        //    GridViewRowEventArgs eSubTotals = new GridViewRowEventArgs(dr);

        //    PickImage(eSubTotals);
        //}
    }

    private void PickImage(GridViewRowEventArgs e)
    {

        string sPlan = "";
        if (e.Row.Cells[1].Text != "&nbsp;")
            sPlan = e.Row.Cells[2].Text.Trim().ToUpper();
        string sProduct = e.Row.Cells[1].Text.Trim().ToUpper();

        if (sProduct == "&NBSP;") return;
        string sColor = string.Empty;
        Image imgChart = GetImage(sPlan, sProduct, ref sColor);


        ALTCHNL_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ALTCHNL_IN"));
        COR_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "COR_IN"));
        CS_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CS_IN"));
        D2D_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "D2D_IN"));
        MOBILITYDMD_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MOBILITYDMD_IN"));
        NATIONALRET_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NATIONALRET_IN"));
        OBTM_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "OBTM_IN"));
        ONLINE_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ONLINE_IN"));
        P2RCALLCEN_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "P2RCALLCEN_IN"));
        STORES_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "STORES_IN"));
        TE_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TE_IN"));
        WEB_IN = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "WEB_IN"));

        ALTCHNL_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ALTCHNL_OUT"));
        COR_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "COR_OUT"));
        CS_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CS_OUT"));
        D2D_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "D2D_OUT"));
        MOBILITYDMD_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MOBILITYDMD_OUT"));
        NATIONALRET_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "NATIONALRET_OUT"));
        OBTM_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "OBTM_OUT"));
        ONLINE_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ONLINE_OUT"));
        P2RCALLCEN_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "P2RCALLCEN_OUT"));
        STORES_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "STORES_OUT"));
        TE_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "TE_OUT"));
        WEB_OUT = -Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "WEB_OUT"));

        //For Net
        ALTCHNL_IN += ALTCHNL_OUT;
        COR_IN += COR_OUT;
        CS_IN += CS_OUT;
        D2D_IN += D2D_OUT;
        MOBILITYDMD_IN += MOBILITYDMD_OUT;
        NATIONALRET_IN += NATIONALRET_OUT;
        OBTM_IN += OBTM_OUT;
        ONLINE_IN += ONLINE_OUT;
        P2RCALLCEN_IN += P2RCALLCEN_OUT;
        STORES_IN += STORES_OUT;
        TE_IN += TE_OUT;
        WEB_IN += WEB_OUT;

        //ALTCHNL_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ALTCHNL_PCT"));
        //COR_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "COR_PCT"));
        //CS_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CS_PCT"));
        //D2D_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "D2D_PCT"));
        //MOBILITYDMD_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "MOBILITYDMD_PCT"));
        //NATIONALRET_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "NATIONALRET_PCT"));
        //OBTM_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "OBTM_PCT"));
        //ONLINE_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "ONLINE_PCT"));
        //P2RCALLCEN_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "P2RCALLCEN_PCT"));
        //STORES_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "STORES_PCT"));
        //TE_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "TE_PCT"));
        //WEB_PCT = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "WEB_PCT"));

        if (sPlan == "&NBSP;")
        {
            ScaleData(e, ref MAX_I, ref MAX_O);

            imgChart.ImageUrl = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=150x160&chco=00ff00|ff00ff|ffff00|c8eb00|00cbcb|e4f7fe|aae2f2|c49be8|dff683|00ffff|0ed653|0000ff,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000&chbh=a&chm=N,000000,0,-1,11&chxt=y&chxl=0:|WEB|TE|STORES|CALLCEN|ONLINE|OBTM|NATL|DMD|D2D|CS|COR|ALTCHNL" +
             "&chd=t:{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}|{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26}&chds={2},{1}&chf=bg,s,{0}",
             sColor, MAX_I, MAX_O, ALTCHNL_IN, COR_IN, CS_IN, D2D_IN, MOBILITYDMD_IN, NATIONALRET_IN, OBTM_IN, ONLINE_IN, P2RCALLCEN_IN, STORES_IN, TE_IN, WEB_IN, ALTCHNL_OUT, COR_OUT, CS_OUT, D2D_OUT, MOBILITYDMD_OUT, NATIONALRET_OUT, OBTM_OUT, ONLINE_OUT, P2RCALLCEN_OUT, STORES_OUT, TE_OUT, WEB_OUT);
        }
        else
            imgChart.ImageUrl = string.Format("http://chart.apis.google.com/chart?cht=bhs&chs=100x160&chco=00ff00|ff00ff|ffff00|c8eb00|00cbcb|e4f7fe|aae2f2|c49be8|dff683|00ffff|0ed653|0000ff,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000,ff0000&chbh=a&chm=N,000000,0,-1,11" +
             "&chxt=y&chxl=0:|*|*|*|*|*|*|*|*|*|*|*|*" +
             "&chd=t:{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14}|{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},{26}&chds={2},{1}&chf=bg,s,{0}",
             sColor, MAX_I, MAX_O, ALTCHNL_IN, COR_IN, CS_IN, D2D_IN, MOBILITYDMD_IN, NATIONALRET_IN, OBTM_IN, ONLINE_IN, P2RCALLCEN_IN, STORES_IN, TE_IN, WEB_IN, ALTCHNL_OUT, COR_OUT, CS_OUT, D2D_OUT, MOBILITYDMD_OUT, NATIONALRET_OUT, OBTM_OUT, ONLINE_OUT, P2RCALLCEN_OUT, STORES_OUT, TE_OUT, WEB_OUT);

        imgChart.Visible = true;
    }

    private static void ScaleData(GridViewRowEventArgs e, ref Int32 MAX_I, ref Int32 MAX_O)
    {
        for (int i = 3; i < e.Row.Cells.Count - 5; i += 3)
        {
            int O = 0, I = 0;
            if (((System.Data.DataRowView)(e.Row.DataItem)) != null)
            {

                if (((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[i] != System.DBNull.Value)
                    I = Convert.ToInt32(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[i]);

                if (((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[i + 1] != System.DBNull.Value)
                    O = Convert.ToInt32(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[i + 2]);

                I -= O;
            }
            MAX_I = I > MAX_I ? I : MAX_I;
            MAX_O = O > MAX_O ? I : MAX_O;
        }
        MAX_I = ((MAX_I / 10) + 1) * 10;
        MAX_O = -MAX_O;
        MAX_O = 0;
    }

    private Image GetImage(string sPlan, string sProduct, ref string sColor)
    {
        Image imgChart = new Image();
        ArrayList alPlans;
        Image[] oaImages;

        switch (sProduct)
        {
            case "IPTV":
                alPlans = new ArrayList(new string[11] { "&NBSP;", "U100", "U200", "U200 LATINO", "U300", "U400", "UFAMILY", "U450", "BP200", "UBASIC", "VACHLD" });
                oaImages = new Image[11] { imgIPTV, imgIPTV_U100, imgIPTV_U200, imgIPTV_U200LA, imgIPTV_U300, imgIPTV_U400, imgIPTV_UFAM, imgIPTV_U450, imgIPTV_BP200, imgIPTV_UBASIC, imgIPTV_VACHLD };
                sColor = sPlan == "SUBTOTAL" ? "cee2fb" : "aec2db";
                if (alPlans.IndexOf(sPlan) > -1)
                    imgChart = oaImages[alPlans.IndexOf(sPlan)];
                else System.Diagnostics.Debug.Print("NEW IPTV Plan:" + sPlan);
                break;

            case "HSIA":
                alPlans = new ArrayList(new string[7] { "&NBSP;", "ELITE", "EXPRESS", "MAX18", "HSIA24", "MAX", "PRO" });
                oaImages = new Image[7] { imgHSIA, imgHSIA_ELITE, imgHSIA_EXPRES, imgHSIA_HSIA18, imgHSIA_HSIA24, imgHSIA_MAX, imgHSIA_PRO };
                sColor = sPlan == "SUBTOTAL" ? "ddf5ac" : "bdd58c";
                if (alPlans.IndexOf(sPlan) > -1)
                    imgChart = oaImages[alPlans.IndexOf(sPlan)];
                else System.Diagnostics.Debug.Print("NEW HSIA Plan:" + sPlan);
                break;

            case "VOIP":
                alPlans = new ArrayList(new string[3] { "&NBSP;", "VOIP 250", "VOIP UNLIMITED" });
                oaImages = new Image[3] { imgVOIP, imgVOIP_BOT250, imgVOIP_UNLMTD };
                sColor = sPlan == "SUBTOTAL" ? "fae5c3" : "e2c5a3";
                if (alPlans.IndexOf(sPlan) > -1)
                    imgChart = oaImages[alPlans.IndexOf(sPlan)];
                else System.Diagnostics.Debug.Print("NEW VOIP Plan:" + sPlan);
                break;

            default:
                break;
        }

        return imgChart;
    }

}



